<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <div id="app"></div>
</body>
<script>
  // 事件触发器
  class EventEmitter {
    constructor() {
      // {click: [fn1,fn2], 'change': [fn]}
      this.subs = Object.create(null)
    }
    // 注册事件
    $on(eventType, handler) {
      this.subs[eventType] = this.subs[eventType] || []
      this.subs[eventType].push(handler)
    }
    // 触发事件 
    $emit(eventType) {
      if (this.subs[eventType]) {
        this.subs[eventType].forEach(handler => {
          handler()
        })
      }
    }
  }

  let test = new EventEmitter()
  test.$on('click', () => {
    console.log('click1')
  })
  test.$on('click', () => {
    console.log('click2')
  })
  test.$emit('click', () => {
    console.log('emit')
  })
</script>

</html>